#include <iostream>
#include <cstdio>
#include <cmath>

using namespace std;

int isleapYear(int y)
{
    if((y%400==0)||(y%4==0&&y%100!=0))
    {
        return 1;
    }
    else
    {
        return 0;
    }
}

int _gcd(int a,int b)
{
    int c;
    if(a<b)
    {
        c=a;
        a=b;
        b=c;
    }
    if(a%b==0)
    {
        return b;
    }
    return _gcd(b,a%b);
}

int _lcm(int a,int b,int c)
{
    return a*b/c;
}

int main(void)
{
    int n;
    int isleap;
    int year[1000][2];
    int gcd,lcm,y,gl,mubiao,tm,td;
    int d1[13]= {0,31,29,31,30,31,30,31,31,30,31,30,31};
    int d2[13]= {0,31,28,31,30,31,30,31,31,30,31,30,31};
    int i,j,k=0,c;
    cin>>n;
    while(n--)
    {
        year[0][0]=0;
        k++;
        c=0;
        mubiao=0;
        cin>>gcd>>lcm>>y;
        gl=gcd*lcm;
        for(i=gcd; i<=lcm; i++)
        {
            for(j=i; j<=lcm; j++)
            {
                if(i*j==gl)
                {
                    if(_gcd(i,j)==gcd&&_lcm(i,j,gcd)==lcm)
                    {
                        year[c][0]=i;
                        year[c][1]=j;
                        c++;
                    }
                }
            }
        }
        if(c==0)
            c++;
        isleap=isleapYear(y);
        //cout<<year[1][0]<<" "<<year[1][1]<<endl;
        for(i=0; i<c; i++)
        {
            if(year[i][0]==0)
            {
                printf("Case #%d: -1\n",k);
                mubiao=-3;
                break;
            }
            if(year[i][0]==year[i][1]&&year[i][0]<=12)
            {
                printf("Case #%d: %d/%02d/%02d\n",k,y,year[i][0],year[i][1]);
                mubiao=-1;
                break;
            }
            if(year[i][0]<=12&&year[i][1]<=12)
            {
                printf("Case #%d: 1\n",k);
                mubiao=-2;
                break;
            }
            if(year[i][0]>12||year[i][1]>31)
            {
                //printf("Case #%d: -1\n",k);
                continue;
            }
            if(year[i][1]>12&&year[i][1]<=31)
            {
                if(isleap)
                {
                    if(year[i][1]<=d1[year[i][0]])
                    {
                        //printf("Case #%d: %d/%02d/%02d\n",k,y,year[i][0],year[i][1]);
                        mubiao++;
                        tm=year[i][0];
                        td=year[i][1];
                        continue;
                    }
                    else
                    {
                        //printf("Case #%d: -1\n",k);
                        continue;
                    }
                }
                else
                {
                    if(year[i][1]<=d2[year[i][0]])
                    {
                        //printf("Case #%d: %d/%02d/%02d\n",k,y,year[i][0],year[i][1]);
                        mubiao++;
                        tm=year[i][0];
                        td=year[i][1];
                        continue;
                    }
                    else
                    {
                        //printf("Case #%d: -1\n",k);
                        continue;
                    }
                }
            }
        }
        if(!mubiao)
        {
            printf("Case #%d: -1\n",k);
            continue;
        }
        if(mubiao==1)
        {
            printf("Case #%d: %d/%02d/%02d\n",k,y,tm,td);
            continue;
        }else
        {
            if(mubiao!=-1&&mubiao!=-2&&mubiao!=-3)
            {
                printf("Case #%d: 1\n",k);
            }
        }
    }
    return 0;
}
